<%--
  User: 16505
  Date: 2020/2/18
  Time: 9:23
疫情看板的页面，是callback后导航过来的
--%>
<%@ page import="java.util.UUID" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>数据看板授权</title>
    <link rel="stylesheet" href="/weui.min.css">
    <link rel="stylesheet" href="/static/jquery-weui.min.css">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
</head>
<body>
<div class="weui-panel weui-panel_access">
    <%--    <div class="weui-panel__hd">图文组合列表</div>--%>
    <div class="weui-panel__bd">
        <a href="javascript:void(0);" class="weui-media-box weui-media-box_appmsg">
            <div class="weui-media-box__hd">
                <img class="weui-media-box__thumb" src="${info.headimgurl}">
            </div>
            <div class="weui-media-box__bd">
                <h4 class="weui-media-box__title">${info.nickname}</h4>
                <%--                <p class="weui-media-box__desc">由各种物质组成的巨型球状天体，叫做星球。星球有一定的形状，有自己的运行轨道。</p>--%>
            </div>
        </a>
    </div>
    <%--</div>--%>
    <%--<div class="weui-cells weui-cells_form">--%>
    <div class="weui-cell">
        <div class="weui-cell__hd"><label class="weui-label">用户姓名</label></div>
        <div class="weui-cell__bd">
            <input id="inputUserName" class="weui-input" type="text" placeholder="请输入用户姓名">
        </div>
    </div>
    <div class="weui-cell">
        <div class="weui-cell__hd"><label class="weui-label">手机号码</label></div>
        <div class="weui-cell__bd">
            <input id="inputPhoneNumber" class="weui-input" type="text" placeholder="请输入手机号码">
            <input id="openid" class="weui-input" type="hidden" value="${info.openid}" placeholder="请输入openid">
            <input id="unionid" class="weui-input" type="hidden" value="${info.unionid}" placeholder="请输入unionid">
            <input id="nickname" class="weui-input" type="hidden" value="${info.nickname}" placeholder="请输入unionid">
        </div>
    </div>
</div>
<div class="button-sp-area">
    <a href="javascript:;" id="btnAuth" class="weui-btn weui-btn_mini weui-btn_primary" onclick="registerWxUser()">申请授权</a>
    <a href="javascript:;" id="btnPerson" class="weui-btn weui-btn_mini weui-btn_primary" onclick="loginPersonGoHome()">返乡数据</a>
    <a href="javascript:;" id="btnIsolation" class="weui-btn weui-btn_mini weui-btn_primary" onclick="isolation()">隔离数据</a>
    <a href="javascript:;" id="btnSampling" class="weui-btn weui-btn_mini weui-btn_primary"
       onclick="caiyangData()">采样数据</a>
    <%--    <a href="javascript:;" class="weui-btn weui-btn_disabled weui-btn_mini weui-btn_primary"--%>
    <%--       onclick="controlButton()">按钮控制</a>--%>
</div>
<%
    UUID uuid = UUID.randomUUID();
%>
<div class="weui-cell">
    <div class="weui-media-box__hd">
        <img width="150px" class="weui-media-box__thumb" src="qrcode?uuid=<%=uuid%>">
    </div>

    <div class="weui-cell__bd">
        <div class="weui-cells__title">软件建议</div>
        <textarea id="suggest" class="weui-textarea" placeholder="" rows="3"></textarea>

        <div class="weui-textarea-counter"><span id="countSuggest">0</span>/200</div>

    </div>
</div>
<div class="button-sp-area">
    <button href="javascript:;" id="btnSuggest" class="weui-btn weui-btn_mini weui-btn_primary" onclick="postSugguest()">
        提交建议
    </button>
</div>
<div class="weui-footer">
    <p class="weui-footer__text">Copyright © 2020 沂水县卫生健康局</p>
</div>

<script src="/jquery.min.js"></script>
<script src="/jquery-weui.min.js"></script>
<script src="/swiper.min.js"></script>
<script src="/city-picker.min.js"></script>
<script>
    $(document).ready(function () {
        $("#btnPerson").hide();
        $("#btnIsolation").hide();
        $("#btnSampling").hide();
        getPower($("#openid").val());
        getSuggest($("#openid").val());
    });
    $(function(){
        var max = 200;
        $('#suggest').on('input', function(){
            var text = $(this).val();
            var len = text.length;
            $('#countSuggest').text(len);
            if(len > max){
                $(this).closest('.weui_cell').addClass('weui_cell_warn');
            }
            else{
                $(this).closest('.weui_cell').removeClass('weui_cell_warn');
            }
        });
    })
    function postSugguest() {
        var jsonData = JSON.stringify({
            openid: $("#openid").val(),
            suggest: $("#suggest").val()
        });
        console.log(jsonData);
        $.ajax({
            url: '/PostGuggest',
            type: 'post',
            dataType: 'json',
            data: jsonData,
            contentType: "application/json",
            async: false,
            timeout: 50000,
            success: function (data) {
                console.log(data);
                $.alert("感谢您的反馈。");
            },
            error: function (msg) {
                console.log(msg);
            }
        });
    }
    function registerWxUser() {
        var name1 = /^[\u4e00-\u9fa5\u4dae\uE863\-a-zA-Z]{1,50}$/; //中英文1到50字内，不带数字
        var name2 = /^(\w)\1{1,}$/; //单个字母，重复字母
        var name3 = ['傻逼', '煞笔', '神经', '艳遇'];
        var name4 = /^[a-zA-Z-\s]+$/;//单个字母

        var mobilereg1 = /^1[3|5|6|7|8|9][0-9]{9}$/; // 1[356789][0-9] （后面再接8位数）
        var mobilereg2 = /^14[5|7][0-9]{8}$/; // 14[57] （后面再接8位数）
        var userName = $.trim($('#inputUserName').val());
        var mobile = $.trim($('#inputPhoneNumber').val());

        if (!userName) {
            $.alert('请填写姓名。');
            return;
        } else if (!name1.test(userName)) {
            $.alert("您输入的姓名格式不正确。");
            return;
        } else if (name2.test(userName.toLocaleLowerCase())) {// 大写字母转小写
            $.alert("您输入的姓名格式不正确。");
            return;
        } else if (name3.indexOf(userName) > -1) {
            $.alert("您输入的姓名格式不正确。");
            return;
        } else if (userName.length <= 1 && name4.test(userName)) {
            $.alert("您输入的姓名格式不正确。");
            return;
        }
        if (!mobile) {
            $.alert('请填写手机号码。');
            return;
        } else if (!mobilereg1.test(mobile) && !mobilereg2.test(mobile)) {
            $.alert('请填写正确的手机号码。');
            return;
        }
        var jsonData = JSON.stringify({
            username: userName,
            phoneNumber: mobile,
            nickname: $("#nickname").val(),
            openid: $("#openid").val(),
            unionid: $("#unionid").val()
        });
        console.log(jsonData);
        postWxUser(jsonData);
        $.alert("申请成功，<br>请与技术团队确认数据权限。");
        getPower($("#openid").val());

    }

    function postWxUser(_data) {
        $.ajax({
            url: '/postone',
            type: 'post',
            dataType: 'json',
            data: _data,
            contentType: "application/json",
            async: false,
            timeout: 50000,
            success: function (data) {
                console.log(data);
            },
            error: function (msg) {
                console.log(msg);
            }
        });
    }

    //get请求，参数放在URL后面和data里面都是一样的
    function getPower(_openid) {
        console.log(_openid);
        $.ajax({
            url: '/getAuthorization',
            type: 'GET',
            dataType: 'json',
            data: {openid: _openid},
            async: false,
            timeout: 50000,
            success: function (data) {
                console.log(data);
                if (data.success) {
                    $("#btnAuth").hide();

                    $("#inputUserName").val(data.result.username);
                    $("#inputUserName").attr("readonly", "readonly");
                    $("#inputPhoneNumber").val(data.result.phoneNumber);
                    $("#inputPhoneNumber").attr("readonly", "readonly");
                    if (isEmpty(data.result.authorization)) {
                        return;
                    }

                    if (data.result.authorization.indexOf("返乡") != -1) {
                        $("#btnPerson").show();
                    }
                    if (data.result.authorization.indexOf("隔离") != -1) {
                        $("#btnIsolation").show();
                    }
                    if (data.result.authorization.indexOf("采样") != -1) {
                        $("#btnSampling").show();
                    }
                } else {
                    $("#btnAuth").show();
                }
            },
            error: function (msg) {
                console.log(msg);
            }
        });
    }

    //get请求，参数放在URL后面和data里面都是一样的
    function getSuggest(_openid) {
        console.log(_openid);
        $.ajax({
            url: '/GetGuggest',
            type: 'GET',
            dataType: 'json',
            data: {openid: _openid},
            async: false,
            timeout: 50000,
            success: function (data) {
                console.log(data);
                if (data.success) {
                    if (isEmpty(data.result.suggest)) {
                        return;
                    }
                    $("#suggest").val(data.result.suggest);
                } else {
                }
            },
            error: function (msg) {
                console.log(msg);
            }
        });
    }


    function loginPersonGoHome() {
        postLog("打开返乡人员");
        commLogin("yslitao","xxhjs123",
            "http://bi.jkgly.cn/WebReport/ReportServer?op=fr_bi&cmd=bi_init&id=144&createBy=-999")
    }
    function isolation() {
        postLog("打开隔离人员");
        commLogin("yslitao","xxhjs123",
            "http://bi.jkgly.cn/WebReport/ReportServer?op=fr_bi&cmd=bi_init&id=47&createBy=102")
    }
    function caiyangData() {
        postLog("打开采样数据");
        commLogin("yslitao","xxhjs123",
            "http://bi.jkgly.cn:80/WebReport/ReportServer?op=fr_bi&cmd=bi_init&id=48&createBy=103")
    }
    function commLogin(_username,_password,_url) {
        var username = _username;
        var password = _password;
        var switch_url = _url
        jQuery.ajax({
            url: "http://bi.jkgly.cn/WebReport/ReportServer?op=fs_load&cmd=sso",//单点登录的报表服务器
            dataType: "jsonp",//跨域采用jsonp方式
            data: {"fr_username": username, "fr_password": password},
            jsonp: "callback",
            timeout: 5000,//超时时间（单位：毫秒）
            success: function (data) {
                if (data.status === "success") {
                    // alert("success"); //登录成功
                    data.url = switch_url //赋值跳转的url，上面var生成，方便管理
                    window.location = data.url;//登录成功，直接跳转到平台系统页面
                } else if (data.status === "fail") {
                    $.alert("用户名密码错误！！！");           //登录失败（用户名或密码错误）
                }
            },
            error: function (msg) {
                console.log(msg);
                // $.alert(msg);// 登录失败（超时或服务器其他错误）
            }
        });
    }
    function isEmpty(obj) {
        if (typeof obj == "undefined" || obj == null || obj == "") {
            return true;
        } else {
            return false;
        }
    }
    function postLog(logContext) {
        var jsonData = JSON.stringify({
            openid: $("#openid").val(),
            eventinfo: logContext
        });
        $.ajax({
            url: '/log/wxUserLog/PostLog',
            type: 'post',
            dataType: 'json',
            data: jsonData,
            contentType: "application/json",
            async: false,
            timeout: 50000,
            success: function (data) {
                console.log(data);
            },
            error: function (msg) {
                console.log(msg);
            }
        });
    }
</script>

</body>
</html>
